package com.btkjgc.service.impl;

import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AdminUser;
import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.Dictionary;
import com.btkjgc.models.ReturnMoney;
import com.btkjgc.models.extend.ReturnMoneyModel;
import com.btkjgc.models.extend.ReturnMoneyReceiveModel;
import com.btkjgc.service.intf.ReturnMoneyManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

/**
 * Created by _ESE on 2015/2/9.
 */
@Component
public class ReturnMoneyManageService_Impl extends BaseService_Impl
		implements ReturnMoneyManageService_Intf {
 
	static Logger logger = Logger.getLogger(ReturnMoneyManageService_Impl.class);

	@SuppressWarnings("unchecked")
	@Override
	public AjaxObj insertReturnMoneyModel(
			ReturnMoneyModel returnMoneyModel) {
		
		AjaxObj ajaxObj=new AjaxObj();
		ajaxObj.setResult(true);
		ReturnMoney returnMoney = new ReturnMoney();

		
		if(returnMoneyModel.getCartypeRadio()==1){
			returnMoneyModel.setCartypes(null);
		}
		if(returnMoneyModel.getAreaRadio()==1){
			returnMoneyModel.setProvinceids(null);
			returnMoneyModel.setCityids(null);
		}
		if(returnMoneyModel.getConditionType()==4){
			if(returnMoneyModel.getConditionMileage()==null){
				returnMoneyModel.setConditionMileage(0);
			}
		}else {
			returnMoneyModel.setConditionMileage(null);
		}
		if(returnMoneyModel.getConditionType()==3){
			if(returnMoneyModel.getConditionNum()==null){
				returnMoneyModel.setConditionNum(0);
			}
		}else {
			returnMoneyModel.setConditionNum(null);
		}
		try {
			PropertyUtils.copyProperties(returnMoney,
					returnMoneyModel);
			returnMoney.setDel(0);
			returnMoney.setCreatedate(new Date());
			returnMoney.setOperdate(new Date());
			returnMoney.setOperid(getLoginUser().getId());
			returnMoney.setStatus(1);
			HibernateUtil.save(returnMoney);

		} catch (Exception e) {
			e.printStackTrace();
			ajaxObj.setResult(false);
			ajaxObj.setMsg("添加时出错");
		}
		
		return ajaxObj;
	}

	@Override
	public void setReturnMoneyListData(HttpServletRequest request, ModelAndView mv,
			int pageIndex, int totalRecord,
			List<ReturnMoneyModel> ReturnMoneyList) {
		// TODO Auto-generated method stub

 
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
 
		mv.addObject("retur_mnoney_list", ReturnMoneyList);


		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
	}

	@Override
	public void setReturnMoneyReceiveListData(HttpServletRequest request, ModelAndView mv,
			int pageIndex, int totalRecord,
			List<ReturnMoneyReceiveModel> returnMoneyReceiveModelList) {
		// TODO Auto-generated method stub

 
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
		
		

		String sql = " select aut.name value,concat(aut.id,'') 'key'  from account_user_t aut where aut.del = 0 and aut.type=1";

		List<Dictionary> companyList = HibernateUtil.executeSQLQueryList(sql,
				Dictionary.class);
		if (companyList.size() > 0) {
			mv.addObject("companyList", companyList);
		}
 
		mv.addObject("retur_mnoney_receive_list", returnMoneyReceiveModelList);


		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
	}
	
	@Override
	public AjaxObj updateReturnMoneyModel(ReturnMoneyModel  returnMoneyModel) {
		// TODO Auto-generated method stub
		AjaxObj ajaxObj = new AjaxObj();
		ajaxObj.setResult(true);
	 
		try {
			Session session =HibernateUtil.openSession();
			Transaction tx  = session.beginTransaction();
			
			ReturnMoney  returnMoney  =new ReturnMoney();
			PropertyUtils.copyProperties(returnMoney, returnMoneyModel);
			
			if(returnMoneyModel.getCartypeRadio()==1){
				session.createQuery("update from ReturnMoney set cartypes=null where id="+returnMoney.getId()).executeUpdate();
				returnMoney.setCartypes(null);
			}
			if(returnMoneyModel.getAreaRadio()==1){
				session.createQuery("update from ReturnMoney set provinceids=null where id="+returnMoney.getId()).executeUpdate();
				session.createQuery("update from ReturnMoney set cityids=null where id="+returnMoney.getId()).executeUpdate();
				returnMoney.setProvinceids(null);
				returnMoney.setCityids(null);
			}
			if(StringUtils.isEmpty(returnMoneyModel.getComment())){
				session.createQuery("update from ReturnMoney set comment=null where id="+returnMoneyModel.getId()).executeUpdate();
			}
			
			if(returnMoneyModel.getConditionType()!=4){
				session.createQuery("update from ReturnMoney set conditionNum=null where id="+returnMoney.getId()).executeUpdate();
			} 
			if(returnMoneyModel.getConditionType()!=3){
				session.createQuery("update from ReturnMoney set conditionMileage=null where id="+returnMoney.getId()).executeUpdate();
			} 
			returnMoney.setOperdate(new Date());
			returnMoney.setOperid(getLoginUser().getId());
			Query query = session.createQuery(getUpdateHql(returnMoney));
			query.executeUpdate();
			session.flush();
			session.clear();
			tx.commit();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ajaxObj.setResult(false);
			ajaxObj.setMsg("更新时发生错误");

		}

		return ajaxObj;
	}

	@Override
	public void updateDelById(String id) {
		// TODO Auto-generated method stub
		AdminUser adminUser=getLoginUser();
		String hql = "update  return_money_t set del=1,operdate=now(),operid="+adminUser.getId()+" where id =( ? )";
		HibernateUtil.executeSQLUpdate(hql, id);
	}

	@Override
	public void updateStatusById(String id, String status) {
		// TODO Auto-generated method stub
		AdminUser adminUser=getLoginUser();
		String hql = "update  return_money_t set status="+status+",operdate=now(),operid="+adminUser.getId()+" where id =( ? )";
		HibernateUtil.executeSQLUpdate(hql, id);
	}

	@Override
	public void setEditReturnMoneydata(HttpServletRequest request, ModelAndView mv,
			Integer id) {
		
		
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
	 
		ReturnMoneyModel returnMoneyModel=(ReturnMoneyModel) HibernateUtil.executeSQLQueryObject("select * from return_money_t where id ="+id,ReturnMoneyModel.class);
 
		if(StringUtils.isEmpty(returnMoneyModel.getCartypes())){
			returnMoneyModel.setCartypeRadio(1);
		}else {
			returnMoneyModel.setCartypeRadio(2);
		}
		if(StringUtils.isEmpty(returnMoneyModel.getProvinceids())&&StringUtils.isEmpty(returnMoneyModel.getCityids())){
			returnMoneyModel.setAreaRadio(1);
		}else {
			returnMoneyModel.setAreaRadio(2);
		}
		mv.addObject("returnMoneyModel", returnMoneyModel);
		
	}

}
